package com.wkq.database.utils;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ShinnyYang
 * on 2016/12/19.
 */
public class GsonUtil {
    private static Gson gson = null;

    static {
        if (gson == null) {
            gson = new Gson();
        }
    }

    private GsonUtil() {
    }

    /**
     * 转成json
     *
     * @param object
     * @return
     */
    public static String GsonString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 转成list
     * 解决泛型问题
     *
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(String json, Class<T> cls) {
        try {
            Gson gson = new Gson();
            List<T> list = new ArrayList<T>();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public static List<Map<String, String>> GsonToListMaps(String gsonString) {
        List<Map<String, String>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, String>>>() {
                    }.getType());
        }
        for (int i = 0; i < list.size(); i++) {
            for (Map.Entry<String, String> entry : list.get(i).entrySet()) {
                if (entry.getValue() == null) {
                    entry.setValue("");
                }
            }
        }
        return list;
    }


    /**
     * 转成map的
     *
     * @param gsonString
     * @return
     */
    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }

    /**
     * 过滤null
     *
     * @param jsonElement
     * @return
     */
    public static String StrConver(JsonElement jsonElement) {
        if (jsonElement == null || jsonElement.isJsonNull() || jsonElement.equals("null")) {
            return "";
        } else {
            return jsonElement.getAsString();
        }
    }

    public static int IntConver(JsonElement jsonElement) {
        if (jsonElement.isJsonNull()) {
            return 0;
        } else {
            return jsonElement.getAsInt();
        }
    }

    public static JsonObject toJsonObject(String str) {
        return new JsonParser().parse(str).getAsJsonObject();
    }

    public static JsonArray toJsonArray(String str) {
        return new JsonParser().parse(str).getAsJsonArray();
    }

}

